package com.example.memolaga.credentials;

import com.example.memolaga.Constants;
import com.example.memolaga.Functions;
import com.example.memolaga.R;
import com.example.memolaga.password.FunctionsPass;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class CredentialView extends Activity {
	
	static int RESULTCODE = 2;

	private String sKey ;
	 
	@Override
	 protected void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.cred_view);
	  String retrievedData = "0";		//Controllare
	  Bundle bundle = this.getIntent().getExtras();
	  if(bundle != null) {
		  retrievedData = bundle.getString("name");
		  this.sKey = bundle.getString(Constants.getKeyString());
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
          if (preferences.getBoolean(Constants.getPrefStringShowPass(), Constants.getPrefDefaultShowPass()))
          {
              ((ImageView) findViewById(R.id.imgViewCredUser)).setVisibility(View.GONE);
              ((TextView) findViewById(R.id.txtCredUserView)).setInputType(InputType.TYPE_CLASS_NUMBER);
              ((ImageView) findViewById(R.id.imgViewCredPass)).setVisibility(View.GONE);
              ((TextView) findViewById(R.id.txtCredPwdView)).setInputType(InputType.TYPE_CLASS_NUMBER);
          }
          CredentialMaster cred = CredentialManager.getInstance().getCredential(retrievedData);
          ((TextView) findViewById(R.id.txtCredNameView)).setText(retrievedData);
          ((TextView) findViewById(R.id.txtCredUserView)).setText(FunctionsPass.decryptString(this.sKey, cred.getUser()));
          ((ImageView) findViewById(R.id.imgViewCredUser)).setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View view) {
                  if (((TextView) findViewById(R.id.txtCredUserView)).getInputType()!= InputType.TYPE_CLASS_NUMBER)
                  {
                      ((ImageView) findViewById(R.id.imgViewCredUser)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                      ((TextView) findViewById(R.id.txtCredUserView)).setInputType(InputType.TYPE_CLASS_NUMBER);
                  }
                  else
                  {
                      ((ImageView) findViewById(R.id.imgViewCredUser)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                      ((TextView) findViewById(R.id.txtCredUserView)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                  }
              }
          });
          ((ImageView) findViewById(R.id.imgViewCopyUser)).setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View view) { //http://stackoverflow.com/questions/6624763/android-copy-to-clipboard-selected-text-from-a-textview
                  String stringPass = ((TextView) findViewById(R.id.txtCredUserView)).getText().toString();
                  if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                      android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                      clipboard.setText(stringPass);
                  } else {
                      android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                      android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringPass);
                      clipboard.setPrimaryClip(clip);
                  }
                  Functions.showShortToast(getApplicationContext(), getString(R.string.str_UserCopy));
              }
          });
          ((TextView) findViewById(R.id.txtCredPwdView)).setText(FunctionsPass.decryptString(this.sKey, cred.getPass()));
          ((ImageView) findViewById(R.id.imgViewCredPass)).setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View view) {
                  if (((TextView) findViewById(R.id.txtCredPwdView)).getInputType()!= InputType.TYPE_CLASS_NUMBER)
                  {
                      ((ImageView) findViewById(R.id.imgViewCredPass)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                      ((TextView) findViewById(R.id.txtCredPwdView)).setInputType(InputType.TYPE_CLASS_NUMBER);
                  }
                  else
                  {
                      ((ImageView) findViewById(R.id.imgViewCredPass)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                      ((TextView) findViewById(R.id.txtCredPwdView)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                  }
              }
          });
          ((ImageView) findViewById(R.id.imgViewCopyPass)).setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View view) { //http://stackoverflow.com/questions/6624763/android-copy-to-clipboard-selected-text-from-a-textview
                  String stringPass = ((TextView) findViewById(R.id.txtCredPwdView)).getText().toString();
                  if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                      android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                      clipboard.setText(stringPass);
                  } else {
                      android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                      android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringPass);
                      clipboard.setPrimaryClip(clip);
                  }
                  Functions.showShortToast(getApplicationContext(), getString(R.string.str_PassCopy));
              }
          });
          ((TextView) findViewById(R.id.txtCredDateCreatedView)).setText(cred.getDateCreated());
          ((TextView) findViewById(R.id.txtCredExpiryView)).setText(cred.getDataExpiry());
          ((TextView) findViewById(R.id.txtCredDateLastUsedView)).setText(cred.getDateLastUsed());
          ((TextView) findViewById(R.id.txtCredSiteView)).setText(cred.getSite());
          ((TextView) findViewById(R.id.txtCredNoteView)).setText(cred.getNote());

	  }
	  Bundle b = new Bundle();
	  b.putString("Value", retrievedData);
	  Intent intent = new Intent();
	  intent.putExtras(b);
	  setResult(RESULTCODE, intent);
	}

}
